﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using A2DFramework.FileService.Configuration;

namespace A2DFramework.FileService
{
    public class FileService
    {
        private IFileService srvProvider;
        public FileService(IFileService srvProvider)
        {
            this.srvProvider = srvProvider;
        }

        public string Save(string content, string desPath)
        {
        start:
            string path = System.IO.Path.Combine(FileServiceConfiguration.NetworkShareRoot, desPath);
            try
            {
                using (StreamWriter sw = new StreamWriter(path))
                {
                    sw.Write(content);
                    sw.Flush();
                }
                return desPath;
            }
            catch (DirectoryNotFoundException ex)
            {
                string dir = System.IO.Path.GetDirectoryName(path);
                Directory.CreateDirectory(dir);
                goto start;
            }
        }

        public string Save(HttpPostedFile srcFile, string desPath)
        {
        start:
            string path = System.IO.Path.Combine(FileServiceConfiguration.NetworkShareRoot, desPath);
            try
            {
                srvProvider.Save(srcFile, desPath);
                return desPath;
            }
            catch (DirectoryNotFoundException ex)
            {
                string dir = System.IO.Path.GetDirectoryName(path);
                Directory.CreateDirectory(dir);
                goto start;
            }
        }

        public string Save2Folder(HttpPostedFile srcFile, string desFolder, string newFileName = "")
        {
        start:
            string path = desFolder;
            if (string.IsNullOrEmpty(newFileName))
                path = System.IO.Path.Combine(path, srcFile.FileName);
            else
                path = System.IO.Path.Combine(path, newFileName);

            string fullPath = System.IO.Path.Combine(FileServiceConfiguration.NetworkShareRoot, path);
            try
            {
                srvProvider.Save(srcFile, fullPath);
                return path;
            }
            catch (DirectoryNotFoundException ex)
            {
                string dir = System.IO.Path.GetDirectoryName(path);
                Directory.CreateDirectory(dir);
                goto start;
            }
        }

        public void Delete(string filePath)
        {
            string path = System.IO.Path.Combine(FileServiceConfiguration.NetworkShareRoot, filePath);
            srvProvider.Delete(path);
        }

        public string ReadToEnd(string filePath)
        {
            string path = System.IO.Path.Combine(FileServiceConfiguration.NetworkShareRoot, filePath);
            return srvProvider.ReadToEnd(path);
        }

        public List<string> Dir(string subDir)
        {
            string path = System.IO.Path.Combine(FileServiceConfiguration.NetworkShareRoot, subDir);
            return srvProvider.Dir(path);
        }

        private static FileService _instance;
        public static FileService Instance()
        {
            if (_instance == null)
                _instance = new FileService(new Providers.DFSNetworkShareFileServiceProvider());
            return _instance;
        }
    }
}
